#include <stdio.h>
#include <SDL.h>
int main(int argc, char *argv[])
 {
	SDL_Surface *screen;	//This pointer will reference the backbuffer
	SDL_Surface *image;	//This pointer will reference our bitmap sprite
	SDL_Surface *temp;//This pointer will temporarily reference our bitmap 	sprite
	SDL_Rect src, dest;//These rectangles will describe the source and 	destination regions of our blit
	SDL_Surface* helicoRight;
	SDL_Surface* helicoLeft;
	SDL_Surface* helicoUp;
	SDL_Surface* helicoDown;
	SDL_Rect helicoRect;
	int helicoDirection = 0;

    int done = 1;
    SDL_Event event;
	//char pause;

	//We must first initialize the SDL video component, and check for success
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	//Set the video mode to fullscreen 640x480 with 16bit colour and double-	buffering
	screen = SDL_SetVideoMode(945, 588, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (screen == NULL) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}
	//Load the bitmap into a temporary surface, and check for success
	temp = SDL_LoadBMP("img/image.bmp");
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	//Convert the surface to the appropriate display format
	image = SDL_DisplayFormat(temp);

    helicoRight = SDL_LoadBMP("img/right.bmp");
    helicoLeft = SDL_LoadBMP("img/left.bmp");
    helicoUp = SDL_LoadBMP("img/up.bmp");
    helicoDown = SDL_LoadBMP("img/down.bmp");

    helicoRect.x = 0;
    helicoRect.y = 0;
    helicoRect.h = helicoLeft->h;
    helicoRect.w = helicoLeft->w;

	//Construct the source rectangle for our blit
	src.x = 0;
	src.y = 0;
	src.w = image->w;	//Use image->w to display the entire width of the image
	src.h = image->h;	//Use image->h to display the entire height of the image
	//Construct the destination rectangle for our blit
	dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
	dest.y = 0;
	dest.w = image->w;
    dest.h = image->h;
	//Blit the image to the backbuffer
	SDL_FreeSurface(temp);
	helicoRect.x = 0;
    helicoRect.y = 0;
	while(done)
    {
        //Release the temporary surface

        SDL_BlitSurface(image, &src, screen, &dest);
        if(helicoDirection == 0)
        {
            helicoRect.h = helicoRight->h;
            helicoRect.w = helicoRight->w;
            SDL_BlitSurface(helicoRight, NULL, screen, &helicoRect);
        }

        if(helicoDirection == 1)
        {
            helicoRect.h = helicoLeft->h;
            helicoRect.w = helicoLeft->w;
            SDL_BlitSurface(helicoLeft, NULL, screen, &helicoRect);
        }
        if(helicoDirection == 2)
        {
            helicoRect.h = helicoDown->h;
            helicoRect.w = helicoDown->w;
            SDL_BlitSurface(helicoDown, NULL, screen, &helicoRect);
        }

        if(helicoDirection == 3)
        {
            helicoRect.h = helicoUp->h;
            helicoRect.w = helicoUp->w;
            SDL_BlitSurface(helicoUp, NULL, screen, &helicoRect);
        }

        //Flip the backbuffer to the primary Hardware Video Memory
        SDL_Flip(screen);
        //Release the surface


        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
                case SDL_QUIT:
                    done = 1;
                break;
                // check for keypresses
                case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = 0;

                    if (event.key.keysym.sym == SDLK_UP)
                    {
                        helicoDirection = 3;
                        if(helicoRect.y > 0)
                            helicoRect.y -= 10;
                    }

                    if (event.key.keysym.sym == SDLK_DOWN)
                    {
                        helicoDirection = 2;
                        if(helicoRect.y < screen->h - helicoRect.h)
                            helicoRect.y += 10;
                    }
                    if (event.key.keysym.sym == SDLK_RIGHT)
                    {
                        helicoDirection = 0;
                        if(helicoRect.x < screen->w - helicoRect.w)
                            helicoRect.x += 10;
                    }
                    if (event.key.keysym.sym == SDLK_LEFT)
                    {
                        helicoDirection = 1;
                        if(helicoRect.x > 0)
                            helicoRect.x -= 10;
                    }

                }
                break;
            } // end switch
        } // end of message processing
	}

    SDL_FreeSurface(image);
    SDL_FreeSurface(helicoLeft);
    SDL_FreeSurface(helicoRight);
    SDL_FreeSurface(helicoUp);
    SDL_FreeSurface(helicoDown);
	//pause = getchar();
	//Return success!
	return 0;
}
